<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="edition" label="版本号">
				<uni-easyinput placeholder="修改版本号将引起更新" v-model="formData.edition"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="tips" label="更新提示">
				<uni-easyinput placeholder="更新提示" v-model="formData.tips"></uni-easyinput>
			</uni-forms-item>
			<!-- <uni-forms-item name="tipsList" label="滚动提示">
				<uni-easyinput type="textarea" placeholder="多条提示请用#分隔"
					v-model="tipsStr"></uni-easyinput>
			</uni-forms-item>  -->
			<uni-forms-item name="swiper" label="轮播图">
				<uni-file-picker :image-styles="imageStyles" v-model="swiperValue" limit="6" fileMediatype="image"
					mode="grid" @success="success" />
			</uni-forms-item>
			<uni-forms-item name="typeList" label="书籍分类">
				<view v-for="(item,index) in formData.typeList" style="display: flex;
				margin-bottom: 40rpx;">
					<uni-easyinput class="key" placeholder="分类名称" v-model="item.name"></uni-easyinput>
					<uni-easyinput class="value" placeholder="分类(多个分类用“,”隔开)" v-model="item.str"></uni-easyinput>
					<uni-icons type="clear" color="#fa3534" size="30" @click="deleteType(index)" style="cursor: pointer;"></uni-icons>
				</view>
				<button class="uni-button" style="width: 100%;cursor: pointer;" @click="addType">添加</button>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/config.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'config';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"edition": "",
				"tips": "",
				"tipsList": [],
				"typeList": []
			}
			return {
				formData,
				tipsStr: '',
				swiperValue: [],
				imageStyles: {
					width: 100,
					height: 100
				},
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad() {
			this.getDetail()
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			deleteType(index){
				this.formData.typeList.splice(index,1)
			},
			addType() {
				this.formData.typeList.push({
					name:'',
					str:'',
					list:[]
				})
			},
			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					console.log(111)
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				var typeList = []
				for (let type of this.formData.typeList) {
					let list = type.str.split(',')
					if (type.name != '') {
						typeList.push({
							name: type.name,
							list: list
						})
					}
				}
				let tipsList = this.tipsStr.split('#')
				value.typeList = typeList
				value.swiper = []
				this.swiperValue.forEach(item => {
					value.swiper.push(item.url)
				})
				value.tipsList = tipsList
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '修改成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},

			/**
			 * 获取表单数据
			 * @param {Object} 
			 */
			getDetail() {
				uni.showLoading({
					mask: true
				})
				db.collection(dbCollectionName).field("edition,tips,tipsList,typeList,swiper").get().then((res) => {
					const data = res.result.data[0]
					this.formDataId = data._id
					if (data) {
						this.tipsStr = data.tipsList.join('#')
						this.formData = data
						this.formData.typeList.forEach(item => {
							item.str = item.list.join(',')
						})
						this.formData.swiper.forEach(item => {
							let extname = item.substr(-3)
							this.swiperValue.push({
								"name": item,
								"extname": extname,
								"url": item,
							})
						})
					}
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>
<style>
	.key {
		max-width: 180rpx;
		margin-right: 20rpx;
	}

	.value {}
</style>